/**
* $Revision$
* $Date$
*
* This file is part of the Weatherstation-Quick-Access repository software.
* Copyright (C) 2011 Weatherstation-Quick-Access developer team
* See http://code.google.com/p/weatherstation-quick-access/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
**/

package gougeon.android.wsqa;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.os.Parcelable;
import android.util.Log;


public class WeatherstationquickaccessActivity extends Activity {
    private ViewPager awesomePager;
    private static int NUM_AWESOME_VIEWS = 5;
    private Context cxt;
    private Activity activity;
    private AwesomePagerAdapter awesomeAdapter;
    AlertDialog builder;
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.base_menu, menu);
        return true;
    }    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle all of the possible menu actions.
        switch (item.getItemId()) {
        case R.id.menu_about:
        	builder.show();
            break;
        case R.id.menu_license:
        	Intent i = new Intent(); 
        	i.setClassName("gougeon.android.wsqa", "gougeon.android.wsqa.ShowData"); 
        	startActivity(i);
            break;
        case R.id.menu_quit:
            finish();
            break;
        }
        return super.onOptionsItemSelected(item);

    }
    /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    cxt = this;
    activity = this;
    Log.d("wsqa", "WeatherstationquickaccessActivity::OnCreate start");
  
    awesomeAdapter = new AwesomePagerAdapter();
    awesomePager = (ViewPager) findViewById(R.id.awesomepager);
    awesomePager.setAdapter(awesomeAdapter);

    try {
        builder = About.create(this);
        //builder.show();
    } catch (NameNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
   	}

    Log.d("wsqa", "WeatherstationquickaccessActivity::OnCreate done");
}

private class AwesomePagerAdapter extends PagerAdapter{

            
            @Override
            public int getCount() {
                    return NUM_AWESOME_VIEWS;
            }

            private String[][] mStrings= { 
            	{
                    "http://weststerne.kicks-ass.org/weathergraph-local/temp-day.png",
                    "http://weststerne.kicks-ass.org/weathergraph-local/temp-week.png",
                    "http://weststerne.kicks-ass.org/weathergraph-local/temp-month.png",
                    "http://weststerne.kicks-ass.org/weathergraph-local/temp-year.png"
                }, 
                {
                    "http://weststerne.kicks-ass.org/weathergraph-local/humid-day.png",
                    "http://weststerne.kicks-ass.org/weathergraph-local/humid-week.png",
                    "http://weststerne.kicks-ass.org/weathergraph-local/humid-month.png",
                    "http://weststerne.kicks-ass.org/weathergraph-local/humid-year.png"                	
                },
                {
                    "http://weststerne.kicks-ass.org/weathergraph-local/rain-day.png",
                    "http://weststerne.kicks-ass.org/weathergraph-local/rain-week.png",
                    "http://weststerne.kicks-ass.org/weathergraph-local/rain-month.png",
                    "http://weststerne.kicks-ass.org/weathergraph-local/rain-year.png"                	
                },
                {
                    "http://weststerne.kicks-ass.org/weathergraph-local/wind-day.png",
                    "http://weststerne.kicks-ass.org/weathergraph-local/wind-week.png",
                    "http://weststerne.kicks-ass.org/weathergraph-local/wind-month.png",
                    "http://weststerne.kicks-ass.org/weathergraph-local/wind-year.png"                	
                },
                {
                    "http://weststerne.kicks-ass.org/weathergraph-local/wind-dir-day.png",
                    "http://weststerne.kicks-ass.org/weathergraph-local/wind-dir-week.png",
                    "http://weststerne.kicks-ass.org/weathergraph-local/wind-dir-month.png",
                    "http://weststerne.kicks-ass.org/weathergraph-local/wind-dir-year.png"                	
                },
            };
        /**
         * Create the page for the given position.  The adapter is responsible
         * for adding the view to the container given here, although it only
         * must ensure this is done by the time it returns from
         * {@link #finishUpdate()}.
         *
         * @param container The containing View in which the page will be shown.
         * @param position The page position to be instantiated.
         * @return Returns an Object representing the new page.  This does not
         * need to be a View, but can be some other container of the page.
         */
            @Override
            public Object instantiateItem(View collection, int position) {
                    Log.d("wsqa", "WeatherstationquickaccessActivity::AwesomePagerAdapter::instantiateItem "+position+" start");
                    LazyAdapter adapter;

                    //TextView tv = new TextView(cxt);
                    ListView lv = new ListView(cxt);
                    adapter=new LazyAdapter(activity, mStrings[position]);
                    lv.setAdapter(adapter);
                    
                   /* tv.setText("Bonjour PAUG " + position + " : " + getCacheDir() );
                    tv.setTextColor(Color.WHITE);
                    tv.setTextSize(30);*/
                    
                    //((ViewPager) collection).addView(tv,0);
                    ((ViewPager) collection).addView(lv,0);
                    
                    Log.d("wsqa", "WeatherstationquickaccessActivity::AwesomePagerAdapter::instantiateItem "+position+" done");
                    return lv;
            }

        /**
         * Remove a page for the given position.  The adapter is responsible
         * for removing the view from its container, although it only must ensure
         * this is done by the time it returns from {@link #finishUpdate()}.
         *
         * @param container The containing View from which the page will be removed.
         * @param position The page position to be removed.
         * @param object The same object that was returned by
         * {@link #instantiateItem(View, int)}.
         */
            @Override
            public void destroyItem(View collection, int position, Object view) {
                    ((ViewPager) collection).removeView((ListView) view);
            }

            
            
            @Override
            public boolean isViewFromObject(View view, Object object) {
                    return view==((ListView)object);
            }

            
        /**
         * Called when the a change in the shown pages has been completed.  At this
         * point you must ensure that all of the pages have actually been added or
         * removed from the container as appropriate.
         * @param container The containing View which is displaying this adapter's
         * page views.
         */
            @Override
            public void finishUpdate(View arg0) {}
            

            @Override
            public void restoreState(Parcelable arg0, ClassLoader arg1) {}

            @Override
            public Parcelable saveState() {
                    return null;
            }

            @Override
            public void startUpdate(View arg0) {}
    
}

}